package com.hopes.bizz.hazard.support;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopes.bizz.hazard.alarm.service.HazAlarmBrokenService;
import com.hopes.bizz.hazard.alarm.service.HazAlarmHandleRecordService;
import com.hopes.bizz.hazard.alarm.service.HazAlarmOverproofService;
import com.hopes.bizz.hazard.common.constant.HazardDicConstant;
import com.hopes.bizz.hazard.enterprises.bo.HazEnterprisesMsgConfigJSON;
import com.hopes.bizz.hazard.enterprises.entity.HazEnterprisesAlarmConfigEntity;
import com.hopes.bizz.hazard.enterprises.entity.HazEnterprisesMsgEntity;
import com.hopes.bizz.hazard.enterprises.entity.HazEnterprisesRtEntity;
import com.hopes.bizz.hazard.enterprises.service.HazEnterprisesAlarmConfigService;
import com.hopes.bizz.hazard.enterprises.service.HazEnterprisesMsgService;
import com.hopes.bizz.hazard.enterprises.service.HazEnterprisesRtService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author daijie
 * @version 1.0
 * @date 2024/2/2 16:17
 * @Description: 企业风险预警消息生成任务帮助类
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class HazEnterprisesMsgGenerateSupport {

    private final HazAlarmBrokenService hazAlarmBrokenService;
    private final HazAlarmOverproofService hazAlarmOverproofService;
    private final HazAlarmHandleRecordService hazAlarmHandleRecordService;
    private final HazEnterprisesMsgService hazEnterprisesMsgService;
    private final HazEnterprisesRtService hazEnterprisesRtService;
    private final HazEnterprisesAlarmConfigService hazEnterprisesAlarmConfigService;

    public Integer calDisturbanceRateAvg(Long orgId) {
        return 20;
    }

    public Integer calRepeatedAlarmPoints(Long orgId) {
        return 20;
    }

    public Integer calAlarmPointAvg(Long orgId) {
        return 20;
    }

    public Integer calAlarmDurationAvg(Long orgId) {
        return 20;
    }

    public Integer calAlarmHandlingRate(Long orgId) {
        return 20;
    }

    public void updateEnterprisesRt(Long orgId, Integer score, Integer disturbanceRateAvg, Integer repeatedAlarmPoints,
                                    Integer alarmPointAvg, Integer alarmDurationAvg, Integer alarmHandlingRate) {
        boolean update = true;
        HazEnterprisesRtEntity rtEntity = hazEnterprisesRtService.getOne(Wrappers.lambdaQuery(HazEnterprisesRtEntity.class)
                .eq(HazEnterprisesRtEntity::getOrgId, orgId)
        );
        if (rtEntity == null) {
            rtEntity = new HazEnterprisesRtEntity();
            rtEntity.setOrgId(orgId);
            update = false;
        }

        rtEntity.setDateTime(new Date());

        rtEntity.setDisturbanceRateAvg(disturbanceRateAvg);
        rtEntity.setRepeatedAlarmPoints(repeatedAlarmPoints);
        rtEntity.setAlarmPointAvg(alarmPointAvg);
        rtEntity.setAlarmDurationAvg(alarmDurationAvg);
        rtEntity.setAlarmHandlingRate(alarmHandlingRate);

        HazEnterprisesAlarmConfigEntity config = hazEnterprisesAlarmConfigService.getOne(Wrappers.lambdaQuery(HazEnterprisesAlarmConfigEntity.class)
                .eq(HazEnterprisesAlarmConfigEntity::getOrgId, orgId));
        if (config == null) {
            // 查询全局配置
            config = hazEnterprisesAlarmConfigService.getOne(Wrappers.lambdaQuery(HazEnterprisesAlarmConfigEntity.class)
                    .eq(HazEnterprisesAlarmConfigEntity::getDefaultFlag, true));
        }
        if (config == null) {
            log.error("企业风险预警消息生成任务失败！！！全局配置为空！！！");
            return;
        }

        try {
            HazEnterprisesMsgConfigJSON msgConfig = config.getMsgConfig();
            HazEnterprisesMsgEntity msg = null;
            if (msgConfig.getMinRiskLevel().getEnable()) {
                if (score >= msgConfig.getMinRiskLevel().getLeft() && score <= msgConfig.getMinRiskLevel().getRight()) {
                    rtEntity.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MIN);

                    msg = new HazEnterprisesMsgEntity();
                    msg.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MIN);
                }
            } else if (msgConfig.getGeneralRiskLevel().getEnable()) {
                if (score >= msgConfig.getGeneralRiskLevel().getLeft() && score <= msgConfig.getGeneralRiskLevel().getRight()) {
                    rtEntity.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_GENERAL);

                    msg = new HazEnterprisesMsgEntity();
                    msg.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_GENERAL);
                }
            } else if (msgConfig.getMoreRiskLevel().getEnable()) {
                if (score >= msgConfig.getMoreRiskLevel().getLeft() && score <= msgConfig.getMoreRiskLevel().getRight()) {
                    rtEntity.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MORE);

                    msg = new HazEnterprisesMsgEntity();
                    msg.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MORE);
                }
            } else if (msgConfig.getMaxRiskLevel().getEnable()) {
                if (score >= msgConfig.getMaxRiskLevel().getLeft() && score <= msgConfig.getMaxRiskLevel().getRight()) {
                    rtEntity.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MAX);

                    msg = new HazEnterprisesMsgEntity();
                    msg.setRiskLevel(HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MAX);
                }
            }

            if (msg != null) {
                msg.setOrgId(orgId);
                msg.setAlarmTime(new Date());
                msg.setHandleStatus(HazardDicConstant.HAZ_ENTERPRISES_ALARM_HANDLE_STATUS_TODO);
                generateAlarmContent(msg);
                hazEnterprisesMsgService.save(msg);
            }
        } catch (NullPointerException nullPointerException) {
            log.error("企业风险预警消息生成任务失败！！！存在配置为空！！！");
            return;
        }

        if (update) {
            hazEnterprisesRtService.updateById(rtEntity);
        } else {
            hazEnterprisesRtService.save(rtEntity);
        }
    }

    public void generateAlarmContent(HazEnterprisesMsgEntity entity) {
        String riskLevel = entity.getRiskLevel();
        if (HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MIN.equals(riskLevel)) {
            entity.setAlarmContent("该企业存在低风险预警!");
        } else if (HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_GENERAL.equals(riskLevel)) {
            entity.setAlarmContent("该企业存在一般风险预警!");
        } else if (HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MORE.equals(riskLevel)) {
            entity.setAlarmContent("该企业存在较大风险预警!");
        } else if (HazardDicConstant.HAZ_ENTERPRISES_RISK_LEVEL_MAX.equals(riskLevel)) {
            entity.setAlarmContent("该企业存在重大风险预警!");
        }
    }
}
